/*
 * This file is part of ksPlatform
 * 
 * Copyright (c) 2014-2015
 * 
 * This file is dual-licensed under the AGPLv3 (http://www.gnu.org/licenses/agpl-3.0.html) 
 * 
 * If you are unsure which license is appropriate for your use, please contact the sales department
 * at 97041437@qq.com.
 */

package cn.com.ksplatform.modules.system.auth.dictmanager.service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import cn.com.ksplatform.base.service.BaseService;
import cn.com.ksplatform.modules.system.auth.dictmanager.model.PlatDictType;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.search.Attribute;
import net.sf.ehcache.search.Query;
import net.sf.ehcache.search.Result;
import net.sf.ehcache.search.Results;

/**
 * @ClassName:PlatDictTypeService <br/>
 * @deprecated:TODO. <br/>
 * @Date: 2015年5月25日 下午2:18:12 <br/>
 * 
 * @author Mr.Luoxing
 * @version 1.0
 */
@Repository("system.modules.system.PlatDictTypeService")
public class PlatDictTypeService extends BaseService{
	
	@Autowired
	@Qualifier("cacheManagerFactory")
	CacheManager cacheManager;
	
	@Transactional
	public PlatDictType save(PlatDictType entity) {
		String id = entity.save();
		entity.setId(id);
		return entity;
	}

	@Transactional
	public PlatDictType update(PlatDictType entity) {
		return entity.update();
	}

	@Transactional
	public PlatDictType delete(PlatDictType entity) {
		return entity.delete();
	}

	@Transactional
	public PlatDictType saveOrUpdate(PlatDictType entity) {
		return entity.saveOrUpdate();
	}
	
	public List getPlatDict(String eCode) {
		List dicts = new ArrayList();
		
		Cache cache = cacheManager.getCache("DictCache");
		
		Query query = cache.createQuery();
		
		Attribute eq = cache.getSearchAttribute("ecode");
		query.addCriteria(eq.eq(eCode));
		
		query.includeValues();
		query.end();
		
		Results result = query.execute();
		
		for(Result r : result.all()){
			dicts.add(r.getValue());
		}
		
		Collections.sort(dicts);
		return dicts;
	}
}
